<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

class Applylist extends Model
{
    protected $table = 'apply_list';
    protected $primaryKey = 'id';

    public static function applyListCheck($corp_id)
    {
        return self::select('apply_list.type','apply_list.reason','apply_list.created_at','corp_user.name','corp_user.avatar','apply_list.type','apply_list.status','apply_list.time_start','apply_list.time_end','apply_list.id')
            ->leftjoin('corp_user', 'apply_list.openid', '=', 'corp_user.openid')
            ->where("apply_list.corp_id", $corp_id)
            ->where("apply_list.status",1)
            ->groupBy("apply_list.id")
            ->orderBy("apply_list.id","DESC")
            ->simplePaginate(10);
    }

    public static function applyList($corp_id,$day,$status)
    {
        $query = self::select('apply_list.type','apply_list.reason','apply_list.created_at','corp_user.name','corp_user.avatar','corp_user.status','apply_list.corp_id')
            ->leftjoin('corp_user', 'apply_list.openid', '=', 'corp_user.openid')
            ->where("corp_user.corp_id", $corp_id);
        if (!empty($day)) $query->where("apply_list.day",$day);
        if (!empty($status)) $query->where("apply_list.status",$status);
        return $query->where("corp_user.status",1)->orderBy("corp_user.id","DESC")->simplePaginate(10);
    }

    public static function userApplyList($openid,$corp_id)
    {
        $query = self::select('apply_list.id','apply_list.type','apply_list.reason','apply_list.created_at','corp_list.avatar','apply_list.status','corp_list.name','apply_list.type','apply_list.corp_id')->leftjoin('corp_list', 'apply_list.corp_id', '=', 'corp_list.corp_id')->where("apply_list.openid", $openid);
        if (!empty($corp_id)) $query->where("apply_list.corp_id", $corp_id);
        return $query->where("apply_list.status","!=",5)->groupBy("apply_list.id")->orderBy("apply_list.id","DESC")->simplePaginate(10);
    }

    public static function userLastApplyList($openid)
    {
        return self::select("id")->where("openid",$openid)->orderBy("id","DESC")->first();
    }

    public static function corpLastApply($corp_id,$openid)
    {
        return self::select("id","status")->where("corp_id",$corp_id)->where("openid",$openid)->orderBy("id","DESC")->first();
    }

    public static function applyListStatus($id,$corp_id,$status,$status_msg)
    {
        return self::where("id",$id)->where("corp_id",$corp_id)->update(["status"=>$status,"status_msg"=>$status_msg]);
    }

    public static function applyDetial($corp_id,$id)
    {
        return self::where("id",$id)->where("corp_id",$corp_id)->first();
    }

    public static function applyCount($corp_id,$openid)
    {
        return self::where("corp_id",$corp_id)->where("openid",$openid)->where("status",2)->sum("id");
    }

    public static function applyResult($openid,$id)
    {
        return self::select("corp_list.name","apply_list.openid","apply_list.corp_id","apply_list.type","apply_list.reason","apply_list.time_start","apply_list.time_end","apply_list.imgage","apply_list.status","apply_list.created_at","corp_list.notice","apply_list.status_msg")
            ->leftjoin('corp_list', 'apply_list.corp_id', '=', 'corp_list.corp_id')
            ->where("apply_list.openid", $openid)
            ->where("apply_list.id", $id)
            ->first();
    }

    public static function applyAdd($openid,$corp_id,$type,$reason,$time_start,$time_end,$imgage,$day)
    {
        return self::insertGetId(["openid"=>$openid,"corp_id"=>$corp_id,"type"=>$type,"reason"=>$reason,"time_start"=>$time_start,"time_end"=>$time_end,"imgage"=>$imgage,"day"=>$day,"status"=>1,"created_at"=>time()]);
    }

    public static function applyUpdate($id,$openid,$corp_id,$type,$reason,$time_start,$time_end,$imgage)
    {
        return self::where("id",$id)->where("openid",$openid)->where("corp_id",$corp_id)->update(["type"=>$type,"reason"=>$reason,"time_start"=>$time_start,"time_end"=>$time_end,"imgage"=>$imgage,"status"=>1]);
    }

}